package com.aprivate.kn.ratingsystem.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.aprivate.kn.ratingsystem.R;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.download.ImageDownloader;

import java.util.List;

/**
 * Created by zhangshaokun on 2018/12/11.
 */

public class ImageAdapter extends android.widget.BaseAdapter {
    private Context mContext;
    private List<String> mImageList;

    public ImageAdapter(Context context, List<String> datas) {
        this.mImageList = datas;
        this.mContext = context;
    }

    @Override
    public int getCount() {
        if (null != mImageList && mImageList.size() > 0) {
            if (mImageList.size() > 9) {
                return 9;
            }
            return mImageList.size() + 1;
        } else {
            return 1;
        }
    }

    @Override
    public Object getItem(int position) {
        if (null != mImageList && mImageList.size() > 0) {
            return mImageList.get(position);
        }
        return "";
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_image_item, null);
            viewHolder.mImageView = convertView.findViewById(R.id.image);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //最后一张显示添加相加按钮
        try {
            if (position + 1 == getCount()) {
                viewHolder.mImageView.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.camera));
            } else {
                ImageLoader.getInstance().displayImage(ImageDownloader.Scheme.FILE.wrap(mImageList.get(position)), viewHolder.mImageView);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;
    }

    class ViewHolder {
        ImageView mImageView;
    }
}
